<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4_3-随鼠标移动的小球</title>
</head>

<body>


    <!--载入vue.js-->
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="./public/js/vue.global.js" type="text/javascript" charset="utf-8"></script>

    <div id="Application">
        <div class="container" @mousemove.stop="move">
            <div class="ball" :style="{left:offsetX+'px', top:offsetY+'px'}">

            </div>
        </div>
    </div>



    <script>
        //定义组件
        const App = {
            //组件的data方法
            data() {
                //返回对象
                return {
                    offsetX: 0,
                    offsetY: 0,
                }
            },
            //计算属性
            computed: {

            },

            //定义组件的函数
            methods: {
                move(event) {
                    //检查右侧不能超出边界
                    if (event.clientX + 30 > 440) {
                        this.offsetX = 440 - 60
                        //检查左侧不能超出边界
                    } else if (event.clientX - 30 < 0) {
                        this.offsetX = 0
                    } else {
                        this.offsetX = event.clientX - 30
                    }

                    //检查下侧不能超出边界
                    if (event.clientY + 30 > 440) {
                        this.offsetY = 440 - 60
                        //检查上侧不能超出边界
                    } else if (event.clientY - 30 < 0) {
                        this.offsetY = 0
                    } else {
                        this.offsetY = event.clientY - 30
                    }
                }
            }
        }

        Vue.createApp(App).mount("#Application")
    </script>

    <style>
        body {
            margin: 0;
            padding: 0;
        }

        .container {
            margin: 0;
            padding: 0;
            position: absolute;
            width: 440px;
            height: 440px;
            background-color: blanchedalmond;
            display: inline;
        }

        .ball {
            position: absolute;
            width: 60px;
            height: 60px;
            left: 100px;
            top: 100px;
            background-color: red;
            border-radius: 30px;
            z-index: 100;
        }
    </style>


</body>

</html>